﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HT.Data.BussinessLogic;
using System.Data;
namespace Hotel.UserControls
{
  public partial class Tax : System.Web.UI.UserControl
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack)
        LoadData();
    }

    private void LoadData()
    {
      try
      {
        DataSet ds = HotelManger.GetInstance().GetAllTax(0);
        if (ds.Tables[0].Rows.Count > 0)
        {
          gvTax.DataSource = ds;
          gvTax.DataBind();
        }
        else
        {
          DataTable dt = new DataTable();
          dt.Columns.Add("TaxId");
          dt.Columns.Add("TaxName");
          dt.Columns.Add("Percentage");
          dt.Columns.Add("Edit");
          dt.Columns.Add("Delete");
          DataRow dr = dt.NewRow();
          dt.Rows.Add(dr);
          gvTax.DataSource = dt;
          gvTax.DataBind();
          gvTax.Rows[0].Visible = false;
        }
      }
      catch (Exception ex)
      {

      }
    }

    protected void gvTax_RowEditing(object sender, GridViewEditEventArgs e)
    {
      gvTax.EditIndex = e.NewEditIndex;
      LoadData();
    }

    protected void gvTax_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {

      gvTax.EditIndex = -1;
      LoadData();
    }

    protected void gvTax_RowCommand(object sender, GridViewCommandEventArgs e)
    {
      if (e.CommandName.Equals("Insert"))
      {
        if (!string.IsNullOrEmpty(((TextBox)gvTax.FooterRow.FindControl("txtNameNew")).Text.Trim()) && !string.IsNullOrEmpty(((TextBox)gvTax.FooterRow.FindControl("txtPercent")).Text.Trim()))
        {
          HotelManger.GetInstance().SaveTax(((TextBox)gvTax.FooterRow.FindControl("txtNameNew")).Text.Trim(), ((TextBox)gvTax.FooterRow.FindControl("txtPercent")).Text.Trim(),"raghu");
          LoadData();
        }
      }

    }

    protected void gvTax_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
      int id = Convert.ToInt32(gvTax.DataKeys[e.RowIndex].Values[0].ToString());
      HotelManger.GetInstance().DeleteTax(id);
      LoadData();
    }

    protected void gvTax_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
      int id = Convert.ToInt32(gvTax.DataKeys[e.RowIndex].Values[0].ToString());
      string name = ((TextBox)gvTax.Rows[e.RowIndex].FindControl("txtName")).Text;
      string percent = ((TextBox)gvTax.Rows[e.RowIndex].FindControl("txtPercent")).Text;
      HotelManger.GetInstance().UpdateTax(id, name, percent,"raghu");
      gvTax.EditIndex = -1;
      LoadData();
    }


  }
}